;;;; 2007-09-08 12:04:17
;;;; Entity class library
(in-package :gtcup)

;;
;; Classe Administrador
;;
(clsql:def-view-class administrador ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (password
    :accessor password
    :type (varchar 50)
    :initarg :password))
  (:base-table administrador))

;;
;; Classe Campeonato
;;
(clsql:def-view-class campeonato ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (corridas
    :accessor corridas
    :db-kind :join
    :db-info (:join-class corrida
			  :home-key id
			  :foreign-key campeonato-id
			  :set t))
   (pontuacoes
    :accessor pontuacoes
    :db-kind :join
    :db-info (:join-class pontuacao
			  :home-key id
			  :foreign-key campeonato-id
			  :set t)))
   (:base-table campeonato))

;;
;; Classe Piloto
;;
(clsql:def-view-class piloto ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (resultados  
    :accessor resultados
    :db-kind :join
    :db-info (:join-class resultado
			  :home-key id
			  :foreign-key piloto-id
			  :set t)))
   (:base-table piloto))

;;
;; Classe Equipe
;;
(clsql:def-view-class equipe ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (resultados  
    :accessor resultados
    :db-kind :join
    :db-info (:join-class resultado
			  :home-key id
			  :foreign-key equipe-id
			  :set t)))
   (:base-table equipe))

;;
;; Classe Carro
;;
(clsql:def-view-class carro ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (resultados  
    :accessor resultados
    :db-kind :join
    :db-info (:join-class resultado
			  :home-key id
			  :foreign-key carro-id
			  :set t)))
   (:base-table carro))



;;
;; Classe Pontuacao
;;
(clsql:def-view-class pontuacao ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (pontos
    :accessor pontos
    :type integer
    :initarg :pontos)
   (posicao
    :accessor posicao
    :type integer
    :initarg :posicao)
   (campeonato-id
    :accessor campeonato-id
    :type integer
    :initarg :campeonato-id))
   (:base-table pontuacao))

;;
;; Classe Circuito
;;
(clsql:def-view-class circuito ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (arquivo
    :accessor arquivo
    :type (varchar 250)
    :initarg :arquivo))
   (:base-table circuito))

;;
;; Classe Corrida
;;
(clsql:def-view-class corrida ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (data
    :accessor data
    :type clsql:wall-time
    :initarg :data)
   (campeonato-id
    :accessor campeonato-id
    :type integer
    :initarg :campeonato-id)
   (campeonato
    :accessor campeonato
    :db-kind :join
    :db-info (:join-class campeonato
			  :home-key campeonato-id
			  :foreign-key id
			  :set nil))
   (resultados  
    :accessor resultados
    :db-kind :join
    :db-info (:join-class resultado
			  :home-key id
			  :foreign-key corrida-id
			  :set t)))
  (:base-table corrida))

;;
;; Classe Resultado
;;
(clsql:def-view-class resultado ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (posicao
    :accessor posicao
    :type integer
    :initarg :posicao)
   (voltas
    :accessor voltas
    :type integer
    :initarg :voltas)
   (melhor-volta
    :accessor melhor-volta
    :type (varchar 20)
    :initarg :melhor-volta)
   (distancia-ultima-volta
     :accessor distancia-ultima-volta
     :type float
     :initarg :distancia-ultima-volta)
   (tempo-corrida
    :accessor tempo-corrida
    :type (varchar 20)
    :initarg :tempo-corrida)
   (piloto-id
    :accessor piloto-id
    :type integer
    :initarg :piloto-id)
   (corrida-id
    :accessor corrida-id
    :type integer
    :initarg :corrida-id)
   (corrida
    :accessor corrida
    :db-kind :join
    :db-info (:join-class corrida
			  :home-key corrida-id
			  :foreign-key id
			  :set nil))
   (piloto
    :accessor piloto
    :db-kind :join
    :db-info (:join-class piloto
			  :home-key piloto-id
			  :foreign-key id
			  :set nil)))
  (:base-table resultado))


;; Modelo das inscricoes

;;
;; Classe Evento
;;
(clsql:def-view-class evento ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome))
   (:base-table evento))

;;
;; Classe Inscricao
;;
(clsql:def-view-class inscricao ()
  ((id
    :accessor id
    :db-kind :key
    :db-constraints :not-null 
    :db-constraints :auto-increment
    :type integer
    :initarg :id)
   (nome
    :accessor nome
    :type (varchar 250)
    :initarg :nome)
   (evento-id
    :accessor evento-id
    :type integer
    :initarg :evento-id))
   (:base-table inscricao))

;;
;; Metodos de apoio
;;

(defgeneric pretty-print (self))

(defmethod pretty-print ((self administrador))
  (format t "~a ~%" 
	  (nome self)))

(defmethod pretty-print ((self campeonato))
  (format t "~a ~%"
	  (nome self)))

(defmethod pretty-print ((self corrida))
  (format t "~a on ~a ~%"
	  (nome self)
	  (data self)))

(defmethod pretty-print ((self piloto))
  (format t "~a ~%"
	  (nome self)))

(defmethod pretty-print ((self resultado))
  (format t "~a ~a ~a ~a ~%"
	  (voltas self)
	  (melhor-volta self)
	  (distancia-ultima-volta self)
	  (tempo-corrida self)))
